package com.los;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

/**
 * @program: java8
 * @description: 如何创建stream
 * @author: Yujuan
 * @create: 2018-06-13 21:58
 **/
public class CreateStream {

    public static void main(String[] args) {
//        createStreamFromCollection().forEach(System.out::println);
//        createStreamFromValues().forEach(System.out::println);
//        createStreamFromArrays().forEach(System.out::println);
//        createStreamFromFiles();
//        createStreamFromIterator().forEach(System.out::println);
        createStreamFromGenarate().forEach(System.out::println);

    }

    private static Stream<String> createStreamFromCollection(){
        List<String> strings= Arrays.asList("Alex","ddd","fdfkjd","ddd");
        return strings.stream();
    }

    private static Stream<String> createStreamFromValues(){
        return Stream.of("dd","dfjisa","dfjkfs","dfdasdf");
    }

    private static Stream<String> createStreamFromArrays(){
        return Arrays.stream(new String[]{"123","haha","Jerry","Honey"});
    }

    private static Stream<String> createStreamFromFiles(){
        Path path= Paths.get("/Users/yujuan/workspace/code/IdeaProjects/qhg-business/qhg-business-api/src/main/java/com/qhg/business/eat/dto/EatPartyDto.java");
        try(Stream<String> lines= Files.lines(path)) {
            lines.forEach(System.out::println);
            return lines;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    private static Stream<Integer> createStreamFromIterator(){
        return Stream.iterate(0,n -> n+2).limit(10);
    }

    private static Stream<Double> createStreamFromGenarate(){
        return Stream.generate(Math::random).limit(10);
    }
}
